package com.df.excel_export.validator.vadator_impl.type_int;

import com.df.excel_export.common.ExcelTable;
import com.df.excel_export.validator.AbstractIFieldValidator;
import com.df.excel_export.validator.IFieldValidator;
import com.df.excel_export.validator.IValidatorFactory;

import java.util.List;

public class IntOpenRangeValidator extends AbstractIFieldValidator implements IValidatorFactory {
    public int min;
    public int max;
    @Override
    public boolean support(String filedType, String ruleStr) {
        if(!filedType.trim().equals("int")){
            return false;
        }
        String trim = ruleStr.trim();
        if(trim.startsWith("range(") && trim.endsWith(")")){
            String substring = trim.substring(5, trim.length() - 1);
            String[] split = substring.split(",");
            if(split.length != 2){
                return false;
            }
            return true;
        }
        return false;
    }

    @Override
    public IFieldValidator create(String filedType, String ruleStr) {
        IntOpenRangeValidator validator  = new IntOpenRangeValidator();
        String trim = ruleStr.trim();
        String newTrim = trim.substring(6, ruleStr.length() - 1);
        String[] split = newTrim.split(",");
        String minStr = split[0];
        String maxStr = split[1];
        if(minStr.equals("-")){
            validator.min = Integer.MIN_VALUE;
        }else {
            validator.min = Integer.parseInt(minStr);
        }
        if(maxStr.equals("-")){
            validator.max = Integer.MAX_VALUE;
        }else {
            validator.max = Integer.parseInt(maxStr);
        }
        return validator;
    }

    @Override
    public Integer getPriority() {
        return 0;
    }

    @Override
    public boolean validate(ExcelTable checkTable,List<Object> rowData, Integer dataDowIndex, Integer fieldIndex, String fieldName, String fieldTypeStr, Object filedValue) {
        Integer value = (Integer) filedValue;
        if(value <= min && value >= max){
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(Integer.MIN_VALUE);
        IntOpenRangeValidator validator = new IntOpenRangeValidator();
        IFieldValidator dator = validator.create("int", "range(100,200");
        boolean validate = dator.validate(null,null,1,1, "", "", 150);
        System.out.println(1);
    }
}
